package com.assumejson.mybatis.factorybean;

import com.assumejson.mybatis.mapper.annotation.MyMapperScan;
import com.assumejson.mybatis.mapper.scan.MyMapperScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/12/8 14:38
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
		// 扫描,@Import注解在那个类上，Spring会将解析该类时的信息携带进来，执行该方法，就非常方便的获取扫描路径了
		Map<String, Object> myMapperScan = importingClassMetadata.getAnnotationAttributes(MyMapperScan.class.getName());
		String path = (String) myMapperScan.get("value");

		MyMapperScanner scanner = new MyMapperScanner(registry);

//		scanner.addIncludeFilter(new TypeFilter() {
//			@Override
//			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//				return true;
//			}
//		});

		scanner.scan(path);
		
	}

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);
	}
}
